Skip to content

Preserve channels-last memory format in to_image - #9637

Open
Hemanath400 wants to merge 1 commit into
pytorch:mainfrom
Hemanath400:fix/to-image-memory-format
Open

Preserve channels-last memory format in to_image#9637
Hemanath400 wants to merge 1 commit into
pytorch:mainfrom
Hemanath400:fix/to-image-memory-format

Conversation

@Hemanath400

Copy link
Copy Markdown

Description

This PR avoids forcing a contiguous copy when converting a NumPy image to a torchvision Image in to_image.

Currently, the NumPy input path uses .contiguous() after permute(), which converts the tensor to a channels-first contiguous memory layout.

Removing .contiguous() preserves the original HWC memory layout while keeping the returned tensor in CHW shape, allowing the resulting tensor to be channels-last compatible.

Changes

  • Remove the unnecessary .contiguous() call from the NumPy input path in to_image.
  • Add a regression test covering channels-last memory format.

Testing

  • TestToImage: 11 passed
  • Regression test for channels-last memory format: passed
  • git diff --check: passed

The full test_transforms_v2.py suite was also run. The unrelated failures were due to the local build not having libjpeg support enabled.

Fixes #9547

@pytorch-bot

pytorch-bot Bot commented Aug 27, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9637

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Aug 27, 2026

Copy link
Copy Markdown

Hi @Hemanath400!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the cla signed label Aug 27, 2026
@meta-cla

meta-cla Bot commented Aug 27, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@Hemanath400
Hemanath400 force-pushed the fix/to-image-memory-format branch from 32d7591 to d9b44cf Compare August 29, 2026 16:07

@fallenmi fallenmi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head d9b44cf1e1f406df2b1f5798926afba4c3e54c51 against exact base ac8d215f7d45d6601451b62e9f81622dac8aa0b4.

Removing the forced contiguous copy preserves the ordinary NumPy HWC storage as a CHW-shaped tensor with channels-last strides. In an independent matrix, standard RGB, RGBA, and float32 HWC arrays become channels-last after adding the batch dimension on this head, while base converts them to contiguous channels-first storage.

I also exercised eight NumPy layouts—including grayscale, row-padded ROIs, strided channels/columns, Fortran order, and float32—through to_image, horizontal flip, resize, dtype conversion, and normalization. Base, head, and the current GitHub merge produced identical shapes and value digests for every applicable operation; the head preserves each input's actual strides rather than changing values. The focused TestToImage slice passes 11/11 on head and merge (10/10 on base), git diff --check is clean, and the current merge tree is byte-identical to the reviewed head.

GitHub currently exposes only the green CLA/import/internal-only checks for this PR, not the normal public test matrix; the exact local functional coverage above is therefore the basis of this approval. I found no blocking issue.

AI disclosure: I used OpenAI Codex to inspect the exact revisions and repository policy, construct and run the stride/layout and downstream-transform matrix, execute the focused tests, and draft this review. I verified the results and conclusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make to_image reading return tensors in CHW

2 participants